package com.qjd.girl.handle;

import com.qjd.girl.aspect.HttpAspect;
import com.qjd.girl.domain.Result;
import com.qjd.girl.enums.ResultEnum;
import com.qjd.girl.expection.GirlExpection;
import com.qjd.girl.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by qiangjindong
 * 2018-08-04 10:43
 */
@ControllerAdvice
public class ExceptionHandle extends RuntimeException {

    private static final Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof GirlExpection) {
            GirlExpection expection = (GirlExpection)e;
            logger.info("【Girl异常】: {}", e);
            return ResultUtil.error(expection.getCode(), expection.getMessage());
        }

        logger.info("【系统异常】: {}", e);
        return ResultUtil.error(ResultEnum.UNKNOWN_ERROR.getCode(), ResultEnum.UNKNOWN_ERROR.getMessage());
    }
}
